#'
#' calculate various basic structures of a population or community
#'
#' @aliases total_abundance total_richnes species_abundance
#' 
#' @usage
#' total_abundance(pop)
#' total_abundance(com)
#' total_richness(com)
#' species_abundance(com)
#' species_list(com)
#' 
#' @param x an population or community object
#' @param pop a population object
#' @param com a community object
#' @param data a spab object retruned from species_abundance function
#'
#' @examples
#' pop=rpopulation()
#' total_abundance(pop)
#' 
#' data(BCI)
#' 
#' species_list(BCI)
#' total_abundance(BCI)
#' total_richness(BCI)
#' da=species_abundance(com)
#' 
#' plot(da) #plot the species abundance rank distribution
#'

#' @export
species_list=function(com){
  return(as.character(levels(com$species)))
}

#' @export
total_abundance=function(x){
  UseMethod("total_abundance")
}
#' @export
total_abundance.population=function(pop){
  if(!inherits(pop,"population"))
    stop("pop is not a population object")
  return(nrow(pop))
}
#' @export
total_abundance.community=function(com){
  if(!inherits(com,"community"))
    stop("com is not a community object")
  return(nrow(com))
}

#' @export
total_richness=function(com){
  if(!inherits(com,"community"))
    stop("com is not a community object")
  return(length(unique(com$species)))
  
}

#' @export
species_abundance=function(com){
  if(!inherits(com,"community"))
    stop("com is not a community object")
  da=table(com$species)
  class(da)="spab"
  return(da)
}

#' @export
plot.spab=function(data){
  require(ggplot2)
  data=data.frame(x=1:length(data),abundance=sort(data,decreasing = TRUE))
  ggplot(data,aes(x=x,y=abundance))+
    geom_step()+
    xlab("Species Rank in Abundance")+
    ylab("Abundance of Ranked Species")
}

